<?php

namespace app\api\controller\kuyou\applet;

use app\api\model\kuyou\applet\Assemble as Assembles;

/**
 * 组装单接口
 */
class Assemble extends Common
{
    protected $noNeedRight = ['getdetail'];

    /**
     * 获取组装单
     * @ApiMethod (POST)
     * @param int $page 页码
     * @param int $limit 每页条数
     * @param string $name 组装单名称
     */
    public function index()
    {
        if ($this->request->isPost()) {
            $page = $this->request->post('page');
            $page = empty($page)?1:$page;
            $limit = $this->request->post('limit');
            $limit = empty($limit)?config('app_limit'):$limit;

            $where = [];
            $code = $this->request->post('purchase_code');
            if(!empty($code)){
                $where['purchase_code'] = ['like','%'.$code.'%'];
            }
            $purchase_time = $this->request->post('purchase_time');
            if(!empty($purchase_time)){
                $str = explode('~',$purchase_time);
                if(isset($str[0]) && isset($str[1])){
                    $where['purchase_time'] = ["BETWEEN", [strtotime($str[0]),strtotime($str[1])]];
                }
            }
            $examine_status = $this->request->post('examine_status');
            if($examine_status!==null && $examine_status!==''){
                $where['examine_status'] = $examine_status;
            }
            $type = $this->request->post('type');
            $type = $type==1?1:2;

            $sort_field = $this->request->post('sort_field');
            $sort_field = empty($sort_field)?'purchase_code':$sort_field;

            $sort_type = $this->request->post('sort_type');
            $sort_type = empty($sort_type)?'DESC':$sort_type;

            $result = Assembles::getList($where,$page,$limit,$type,$sort_field,$sort_type);

            if(isset($result['list']) && isset($result['count'])){
                $this->success('查询成功',$result,200);
            }else{
                $this->error('查询失败','',406);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 获取组装单详情
     * @ApiMethod (POST)
     * @param string $purchase_code
     */
    public function getdetail()
    {
        if ($this->request->isPost()) {
            $purchase_code = $this->request->post('purchase_code');
            if(empty($purchase_code)){
                $this->error('请输入查询参数','',500);
            }
            $result = Assembles::getDetail($purchase_code);
            if($result['status']==200){
                $this->success('查询成功',$result['msg'],200);
            }else{
                $this->error($result['msg'],'',$result['status']);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 添加组装单
     * @ApiMethod (POST)
     * @param array $detail 组装单数据
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            foreach ($data as $k=>$v){
                $data[$k] = is_array($v)?$v:html_entity_decode($v);
            }
            $ret = Assembles::addDetail($data);
            if($ret['status']==200){
                $this->success('添加成功',$ret['msg'],200);
            }else{
                $this->error($ret['msg'],'',$ret['status']);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 更新组装单
     * @ApiMethod (POST)
     * @param array $detail 组装单数据
     */
    public function edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            foreach ($data as $k=>$v){
                $data[$k] = is_array($v)?$v:html_entity_decode($v);
            }
            $ret = Assembles::editDetail($data);
            if($ret['status']==200){
                $this->success('更新成功','',200);
            }else{
                $this->error($ret['msg'],'',$ret['status']);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 删除组装单
     * @ApiMethod (POST)
     * @param int $purchase_code 组装单编号
     */
    public function del()
    {
        if ($this->request->isPost()) {
            $purchase_code = $this->request->post('purchase_code');
            $ret = Assembles::delDetail($purchase_code);
            if($ret['status']==200){
                $this->success('删除成功',$ret['msg'],200);
            }else{
                $this->error($ret['msg'],'',$ret['status']);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 审核组装单
     * @ApiMethod (POST)
     * @param int $purchase_code 组装单编号
     */
    public function examine()
    {
        if ($this->request->isPost()) {
            $purchase_code = $this->request->post('purchase_code');
            $type = $this->request->post('type');
            if($type=='examine'){
                $ret = Assembles::examine($purchase_code);
                $msg = '审核成功';
            }else{
                $ret = Assembles::examinef($purchase_code);
                $msg = '反审核成功';
            }
            if($ret['status']==1){
                $this->success($msg,$ret['msg'],200);
            }else{
                $this->error($ret['msg'],'',$ret['status']);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }
}
